package com.ternnetwork.core.access.controller;


import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ternnetwork.commons.model.http.BaseResponse;
import com.ternnetwork.commons.utils.JsonUtils;
import com.ternnetwork.core.accesscontroll.entity.Department;
import com.ternnetwork.core.accesscontroll.service.DepartmentService;



@Controller
@RequestMapping("/access/department/*")
public class DepartmentController {
	

	private final DepartmentService departmentService;

	public DepartmentController(DepartmentService departmentService) {
		this.departmentService = departmentService;
	}

	@PreAuthorize("hasAnyAuthority('ACCESS_DEPARTMENT_ADD')")
	 @RequestMapping("/add")
     public @ResponseBody BaseResponse add(String requestJson)throws Exception{
    	 Department t=JsonUtils.jsonToObjectThrowsException(requestJson, Department.class);
		 BaseResponse baseResponse=departmentService.add(t);
		 return baseResponse;
    }

	 @PreAuthorize("hasAnyAuthority('ACCESS_DEPARTMENT_UPDATE')")
	 @RequestMapping("/update")
     public @ResponseBody BaseResponse update(String requestJson)  throws Exception{
    	 Department t=JsonUtils.jsonToObjectThrowsException(requestJson, Department.class);
		 BaseResponse baseResponse=departmentService.update(t);
		 return baseResponse;
     }


	 @PreAuthorize("hasAnyAuthority('ACCESS_DEPARTMENT_DELETE')")
	 @RequestMapping("/deleteById")
     public @ResponseBody BaseResponse deleteById(Long id) {
    	 return departmentService.deleteById(id);
     }
	 
	 @RequestMapping("/getZTreeJSON")
     public @ResponseBody BaseResponse findAllByCategory() {
    	 return departmentService.getZTreeJSON();
     }
	 
	
	
	

}
